<?
include_once 'functionsPrjSt.php';
include 'headermenu.php';
	$dbh=dbConn();
	//aici se ia id-proiect din url-ul de legatura catre aceasta pagina
    $idp=$_GET[rowid];
		$subnam=$_GET[subn];
		$parent=$_GET[par];
	
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
         <link rel="stylesheet" href="yui.css" type="text/css">
     
</head>

<body>

<div id="doc" class="yui-t7">

<div class="yui-gc">
    <div class="yui-u first">
	<!-- YOUR DATA GOES HERE -->
    
   <h3> Etape si Subetape ale proiectului - 
   <?
   $resultx = returnQuery("select * from projects where id = '".$idp."';",''); 
				$rowx = mysql_fetch_assoc($resultx);
				
   echo $rowx['name'];
   ?>
   </h3>
   
      <form action="" name="testQR" method="post" >
	<div align="right"> 
    <table> <tr> 
    			<td> <input type="submit" name="adauga_et" value="Adauga etapa" align="right"> </div> </td>
	       		<td> <input type="submit" name="adauga_set" value="Adauga subetapa" align="right"> </div> </td> 
			</tr>
    </table>
     </form>
	    </div>
    <div class="yui-u">
    </div>
 </div>

<div id="bd" role="main">
	<div class="yui-gc">
    <div class="yui-u first">
     
   

 <? 
  echo $_POST['name'];
    echo $_POST['stage'];

  
 if(isset($_POST['add']))
		{
			if ($_POST['name']!=null)
			{
					$result3 = returnQuery("select * from stages where id = '".$_POST['stage']."';",''); 
					if( mysql_num_rows($result3) > 0)
					{
						$row3 = mysql_fetch_assoc($result3);
						//echo "hhhhh".$row3['stage_order'];
				
						$result4 = returnQuery("update stages set stage_order=stage_order + 1 where stage_order > ".$row3['stage_order']." and id_project = ".$idp.";",''); 
						mysql_query("INSERT INTO stages(id, id_project, stage_name, stage_order) VALUES(null,'".$idp."','".$_POST['name']."','".($row3['stage_order']+1)."'); ") or die(mysql_error());
					}
					else mysql_query("INSERT INTO stages(id, id_project, stage_name, stage_order) VALUES(null,'".$idp."','".$_POST['name']."','0'); ") or die(mysql_error());
			
				 
							
			}
		}
			
		
if(isset($_POST['aplica']))
		{
		
			
			if ($_POST['subname']!="")
			{
				$result31 = returnQuery("select * from substages where id = '".$_POST['sel_place']."';",''); 
				if( mysql_num_rows($result31) > 0)
				{
					$row31 = mysql_fetch_assoc($result31);
				
					$result41 = returnQuery("update substages set substage_order=substage_order + 1 where substage_order > ".$row31['substage_order']." and id_stage = ".$_POST['stage_parent'].";",''); 
				
					echo $_POST['subname']." =  nume stage si ".$row31['substage_order']." =stage order  ".$_POST['stage_parent']."= tasu'. ";
				
					mysql_query("INSERT INTO substages(id, id_stage, substage_name, substage_order) VALUES(null, '".$_POST['stage_parent']."', '".$_POST['subname']."','".($row31['substage_order']+1)."'); ") or die(mysql_error());
				}
				else 
				mysql_query("INSERT INTO substages(id, id_stage, substage_name, substage_order) VALUES(null, '".$_POST['stage_parent']."', '".$_POST['subname']."','0'); ") or die(mysql_error());
				
			}} 
		
 
 //zona afisare
 
 
 
 ?>
 
     
    <?
	
	$result = returnQuery("Select * from stages where id_project=".$idp." order by stage_order asc;",$dbh);
		
	//####afisam inregistrarile in tabela:
	echo('<h2><table width=100%>');
	
	while ($row = mysql_fetch_assoc($result)) 
	{
		
		echo "<tr> <th>".$row["stage_name"]."</th></tr>";
		$result2 = returnQuery("Select * from substages where id_stage=".$row['id']." order by substage_order asc;",$dbh);
		//echo "the evil result... ".$result2;
		if( mysql_num_rows($result2) > 0)
				{	echo('<tr> <td align="right"> <table align="right" width=90%>');
					
					while ($row2 = mysql_fetch_assoc($result2)) 
					{
						echo "<tr>  <td> ".$row2["substage_name"]."</td> </tr>";
						//echo "caca";
					}
					echo('</table> </td></tr>');
					mysql_free_result($result2);	
				}
	}
	echo('</table></h2>');
	mysql_free_result($result);	
		
	?>

  	</div>

   
	 <div class="yui-u">
	
	     <? 


if(isset($_POST['adauga_et']))
{
	 display($idp);
}
if(isset($_POST['adauga_set']))
{
	 display_set($idp);
}
if(isset($_POST['aplica']))
{
	$subn=$_POST["subname"];
	echo $subn."xxx";
	// displaysub($subn, $_POST["stage_parent"], $idp);
}
	   
?>
    
    	</div>

	
 
</div>
</body>
</html>

<?
	dbClose($dbh);
include 'footer.php'
?>
